package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "C端-购物车接口")
@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingService shoppingService;


    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车,用户id:{},商品：{}", BaseContext.getCurrentId(), shoppingCartDTO);
        shoppingService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    @ApiOperation("查询购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        Long userId = BaseContext.getCurrentId();
        log.info("查询购物车,用户id:{}", userId);
        List<ShoppingCart> cartList = shoppingService.list(userId);
        return Result.success(cartList);
    }


    @ApiOperation("删除购物车中一个商品")
    @PostMapping("/sub")
    public Result subCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        Long userId = BaseContext.getCurrentId();
        log.info("用户：{}，删除购物车中一个商品:{}", userId, shoppingCartDTO);
        shoppingService.subCart(shoppingCartDTO);
        return Result.success();
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result subCart(){
        Long userId = BaseContext.getCurrentId();
        log.info("用户：{}，清空购物车", userId);
        shoppingService.clean();
        return Result.success();
    }
}
